<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">品牌管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增品牌
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="品牌名称">
          <el-input v-model="searchForm.name" placeholder="请输入品牌名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :data="tableData" border stripe>
        <el-table-column prop="id" label="编号" width="80" />
        <el-table-column prop="code" label="品牌代码" width="120" />
        <el-table-column prop="name" label="品牌名称" width="150" />
        <el-table-column prop="englishName" label="英文名称" width="150" />
        <el-table-column prop="logo" label="品牌Logo" width="100">
          <template #default="{ row }">
            <el-image 
              v-if="row.logo" 
              :src="row.logo" 
              style="width: 50px; height: 50px;"
              :preview-src-list="[row.logo]"
              fit="contain"
            />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="origin" label="原产地" width="120" />
        <el-table-column prop="website" label="官网" width="200" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="150" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
            <el-button 
              link 
              :type="row.status === 1 ? 'warning' : 'success'" 
              @click="handleToggleStatus(row)"
            >
              {{ row.status === 1 ? '停用' : '启用' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSearch"
        @current-change="handleSearch"
      />
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog 
      v-model="dialogVisible" 
      :title="dialogTitle"
      width="600px"
    >
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="品牌代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入品牌代码" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="englishName">
          <el-input v-model="form.englishName" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="品牌Logo" prop="logo">
          <el-input v-model="form.logo" placeholder="请输入Logo URL" />
          <el-image 
            v-if="form.logo" 
            :src="form.logo" 
            style="width: 100px; height: 100px; margin-top: 10px;"
            fit="contain"
          />
        </el-form-item>
        <el-form-item label="原产地" prop="origin">
          <el-input v-model="form.origin" placeholder="请输入原产地" />
        </el-form-item>
        <el-form-item label="官网" prop="website">
          <el-input v-model="form.website" placeholder="请输入官网地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input 
            v-model="form.remark" 
            type="textarea" 
            :rows="3"
            placeholder="请输入备注信息" 
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

// 搜索表单
const searchForm = reactive({
  name: ''
})

// 表格数据
const tableData = ref([
  { id: 1, code: 'APPLE', name: '苹果', englishName: 'Apple', logo: '', origin: '美国', website: 'https://www.apple.com', phone: '400-666-8800', sort: 1, status: 1, remark: '消费电子品牌' },
  { id: 2, code: 'HUAWEI', name: '华为', englishName: 'Huawei', logo: '', origin: '中国', website: 'https://www.huawei.com', phone: '400-830-8300', sort: 2, status: 1, remark: '通信设备品牌' },
  { id: 3, code: 'XIAOMI', name: '小米', englishName: 'Xiaomi', logo: '', origin: '中国', website: 'https://www.mi.com', phone: '400-100-5678', sort: 3, status: 1, remark: '智能硬件品牌' },
  { id: 4, code: 'SAMSUNG', name: '三星', englishName: 'Samsung', logo: '', origin: '韩国', website: 'https://www.samsung.com', phone: '400-810-5858', sort: 4, status: 1, remark: '电子产品品牌' },
  { id: 5, code: 'NIKE', name: '耐克', englishName: 'Nike', logo: '', origin: '美国', website: 'https://www.nike.com', phone: '400-880-6453', sort: 5, status: 1, remark: '运动品牌' },
  { id: 6, code: 'ADIDAS', name: '阿迪达斯', englishName: 'Adidas', logo: '', origin: '德国', website: 'https://www.adidas.com', phone: '400-920-5588', sort: 6, status: 1, remark: '运动品牌' }
])

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 6
})

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('新增品牌')
const formRef = ref(null)

// 表单数据
const form = reactive({
  id: null,
  code: '',
  name: '',
  englishName: '',
  logo: '',
  origin: '',
  website: '',
  phone: '',
  sort: 0,
  status: 1,
  remark: ''
})

// 表单验证规则
const rules = {
  code: [{ required: true, message: '请输入品牌代码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入品牌名称', trigger: 'blur' }]
}

// 搜索
const handleSearch = () => {
  console.log('搜索:', searchForm)
  ElMessage.success('查询成功')
}

// 重置
const handleReset = () => {
  searchForm.name = ''
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增品牌'
  Object.assign(form, {
    id: null,
    code: '',
    name: '',
    englishName: '',
    logo: '',
    origin: '',
    website: '',
    phone: '',
    sort: 0,
    status: 1,
    remark: ''
  })
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑品牌'
  Object.assign(form, { ...row })
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(`确定要删除品牌 "${row.name}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    console.log('删除:', row.id)
    ElMessage.success('删除成功')
  })
}

// 切换状态
const handleToggleStatus = (row) => {
  const status = row.status === 1 ? 0 : 1
  const text = status === 1 ? '启用' : '停用'
  ElMessageBox.confirm(`确定要${text}品牌 "${row.name}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    row.status = status
    ElMessage.success(`${text}成功`)
  })
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log('提交:', form)
      ElMessage.success(form.id ? '修改成功' : '新增成功')
      dialogVisible.value = false
      handleSearch()
    }
  })
}
</script>

<style scoped>
.page-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.search-form {
  margin-bottom: 20px;
}

.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>

